/**
 * 
 */
package com.ms.game.common.container;

import java.util.ArrayList;
import java.util.HashMap;

import com.ms.game.common.GameType;
import com.ms.game.slot.base.GainRateData;
import com.ms.game.slot.base.GainRateSystem;
import com.xpec.c4.game.utility.GameUtility;

/**
 * casino game server 基本設定
 * 
 * @author TW15010
 */
public class BasicSettingUtil extends BasicBinUtil {
	public static BasicSetting bin = null;

	/**
	 * 遊戲類型 to 遊戲類型資訊的 map<br>
	 * {@link GameType} \ {@link GameTypeInfo}
	 */
	private static HashMap<String, GameTypeInfo> gameTypeInfoMap = new HashMap<String, GameTypeInfo>();

	@Override
	public void onBinLoad(Object binInstance) {
		bin = (BasicSetting) binInstance;

		// initial 遊戲類型 to 遊戲類型資訊的 map
		ArrayList<GameTypeInfo> gameTypeInfoList = bin.getGameTypeInfoList();
		for (GameTypeInfo info : gameTypeInfoList) {
			gameTypeInfoMap.put(info.getGameType(), info);
		}
	}

	/**
	 * 取得指定機台編號在座位列表的哪一頁
	 * 
	 * @param machindId
	 *            機台編號 (1 base)
	 * @return
	 */
	public static int getPageNumByMachineId(int machindId) {
		int seatCountPerPage = BasicSetting.getInstance().getPageSize();

		int machindIdMod = machindId % seatCountPerPage;

		int page = ((machindId - machindIdMod) / seatCountPerPage);

		page = machindIdMod > 0 ? page + 1 : page;

		return page;
	}
	
	/**
	 * 取得指定機台編號在座位列表的哪一頁(透過gainRateID查詢)
	 */
	public static int getPageNumByMachineId(String gameType, String hallType, int gainRateID, int machindId) {
		int seatCountPerPage = BasicSetting.getInstance().getPageSize();
		GainRateData gainRateData = GainRateSystem.getGainRateData(gameType, hallType, gainRateID);
		if(gainRateData == null){
			return 0;
		}
		
		int machineIDOffset = machindId - gainRateData.getBeginMachineID() + 1;
		
		int machindIdMod = machineIDOffset % seatCountPerPage;
		int page = ((machineIDOffset - machindIdMod) / seatCountPerPage);
		page = machindIdMod > 0 ? page + 1 : page;
		return page;
	}

	/**
	 * 取得隨機的角色大頭圖 id
	 * 
	 * @return 大頭圖 id
	 */
	public static String getRandomFaceIconId() {
		ArrayList<String> faceIconList = BasicSetting.getInstance().getFaceIconList();

		int cnt = faceIconList.size();
		if (cnt == 0) {
			return null;
		}

		int id = GameUtility.randomInt(0, cnt - 1);
		String faceIconId = faceIconList.get(id);

		return faceIconId;
	}

	/**
	 * 取得指定的遊戲資訊
	 * 
	 * @param gameType
	 *            遊戲名稱
	 * @return
	 */
	public static GameTypeInfo getGameTypeInfo(String gameType) {
		if (!GameUtility.isHaveString(gameType)) {
			return null;
		}

		GameTypeInfo gameInfo = gameTypeInfoMap.get(gameType);

		return gameInfo;
	}
	
	/**
	 * 取得遊戲名稱
	 * @param gameId
	 * @return
	 */
	public static String getGameTypeNameByStringTable(String gameId) {
		if (!GameUtility.isHaveString(gameId)) {
			return null;
		}

		GameTypeInfo gameInfo = gameTypeInfoMap.get(gameId);
		if (gameInfo == null) {
			return gameId;
		}

		String gameTypeName = StringTableBinUtil.getText(gameInfo.getName());
		if(gameTypeName != null){
			return gameTypeName;
		}
		else{
			return gameId;
		}
	}
	
	/**
	 * 取得遊戲名稱
	 * 
	 * @param gameType
	 * @return
	 */
	public static String getGameTypeNameByStringTable(GameType gameType) {
		if (gameType == null){
			return null;
		}
		return getGameTypeNameByStringTable(gameType.name());
	}
}
